package com.yp.loading.base;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.res.Resources;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.DialogFragment;

import com.yp.loading.R;
import com.yp.loading.interfaces.DismissListener;
import com.yp.loading.interfaces.ResultListener;

import java.io.Serializable;

import static android.view.Gravity.CENTER;
import static android.view.ViewGroup.LayoutParams.MATCH_PARENT;
import static android.view.ViewGroup.LayoutParams.WRAP_CONTENT;

/**
 * 作者：刘江飞
 * 时间：2020/6/17
 * 邮箱：642371742@qq.com
 * 描述：Dialog的基类
 */
public abstract class BaseDialog<T> extends DialogFragment {
    protected AppCompatActivity mActivity;
    protected View mRootView;
    private final int mAnimation = R.style.DialogBaseAnimation;
    protected ResultListener<T> mResultListener;
    protected DismissListener mDismissListener;
    private int widgetWidth;
    private int location_x;
    private int widgetHeight;
    private int location_y;
    private int[] dialogLocation;
    private boolean transparent;

    public BaseDialog() {
    }

    public BaseDialog(int widgetWidth, int location_x, int widgetHeight, int location_y) {
        this.widgetWidth = widgetWidth;
        this.location_x = location_x;
        this.widgetHeight = widgetHeight;
        this.location_y = location_y;
        dialogLocation = new int[]{widgetHeight, location_x, widgetHeight, location_y};
    }

    public BaseDialog(int widgetWidth, int location_x, int widgetHeight, int location_y, boolean transparent) {
        this.widgetWidth = widgetWidth;
        this.location_x = location_x;
        this.widgetHeight = widgetHeight;
        this.location_y = location_y;
        dialogLocation = new int[]{widgetHeight, location_x, widgetHeight, location_y};
        this.transparent = transparent;
    }

    @Override
    public void onAttach(@NonNull Context context) {
        super.onAttach(context);
        mActivity = (AppCompatActivity) context;
    }


    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        setStyle();
        mRootView = inflater.inflate(setLayoutId(), container, false);
        return mRootView;
    }

    /**
     * 设置统一样式
     */
    private void setStyle() {
        //获取Window
        Dialog mDialog = getDialog();
        if (mDialog == null) {
            return;
        }
        //无标题
        mDialog.requestWindowFeature(STYLE_NO_TITLE);
        mDialog.setCancelable(isCancelable());
        mDialog.setCanceledOnTouchOutside(isCancelOutside());
        Window window = mDialog.getWindow();
        if (window == null) {
            return;
        }
        // 透明背景
        window.setBackgroundDrawableResource(android.R.color.transparent);
        //设置宽高
        window.getDecorView().setPadding(0, 0, 0, 0);
        WindowManager.LayoutParams wlp = window.getAttributes();
        wlp.gravity = getGravity();
        if (dialogLocation != null) {
            if (transparent) {
                wlp.alpha = 1.0f;
                wlp.dimAmount = 0.0f;
            }
            int notificationBarHeight = Resources.getSystem().getDimensionPixelSize(
                    Resources.getSystem().getIdentifier("status_bar_height", "dimen", "android"));

            wlp.x = location_x;
            wlp.y = location_y + widgetHeight - notificationBarHeight;
            wlp.width = widgetWidth;
            wlp.height = WRAP_CONTENT;
        } else {
            wlp.width = MATCH_PARENT;
            wlp.height = MATCH_PARENT;
        }

        //设置对齐方式
        //设置偏移量
        //设置动画
        window.setWindowAnimations(mAnimation);
        window.setAttributes(wlp);
    }

    @Override
    public abstract boolean isCancelable();

    protected abstract boolean isCancelOutside();

    protected int getGravity() {
        return CENTER;
    }

    @Override
    public void onDismiss(@NonNull DialogInterface dialog) {
        super.onDismiss(dialog);
        if (mDismissListener != null) {
            mDismissListener.dismiss(this);
        }
    }

    public void setDialogResultListener(ResultListener<T> resultListener) {
        this.mResultListener = resultListener;
    }

    public void setDialogDismissListener(DismissListener dismissListener) {
        this.mDismissListener = dismissListener;
    }


    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initView();
        initData();
        setListener();
        getDialog().setOnKeyListener(new DialogInterface.OnKeyListener() {
            @Override
            public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
                return keyCode == KeyEvent.KEYCODE_BACK;
            }
        });
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }


    /**
     * Sets layout id.
     *
     * @return the layout id
     */
    protected abstract int setLayoutId();

    /**
     * 初始化数据
     */
    protected void initData() {

    }

    /**
     * view与数据绑定
     */
    protected void initView() {

    }

    /**
     * 设置监听
     */
    protected void setListener() {

    }

    protected Serializable getSerializableValue(String key) {
        Bundle mBundle = getArguments();
        if (mBundle == null) {
            return null;
        }
        return mBundle.getSerializable(key);
    }
}
